﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 殭屍待機狀態
/// </summary>
public class ZombieState_Wait : StateBase<Zombie>
{

    public ZombieState_Wait(string name)
    {
        this.StateName = name;
    }

    public override void StateEnter(Zombie entity)
    {
        Debug.Log(Time.time+"殭屍進入待機狀態");
    }

    public override void StateExit(Zombie entity)
    {
        Debug.Log(Time.time + "殭屍離開待機狀態");
    }

    public override void Update(Zombie entity)
    {
        View(entity);
    }

    /// <summary>
    /// 殭屍視野
    /// </summary>
    private void View(Zombie zb)
    {
        Vector3 left = Quaternion.Euler(0, -zb.viewAngle / 2, 0) * zb.transform.forward * zb.viewRadius;
        for(int i = 0; i <= zb.viewAngeleStep; i++)
        {
            Vector3 v = Quaternion.Euler(0, (zb.viewAngle / zb.viewAngeleStep) * i, 0) * left;
            Ray ray = new Ray(zb.eye.position, v);
            RaycastHit hit = new RaycastHit();
            Physics.Raycast(ray, out hit, zb.viewRadius);
            Vector3 position = zb.eye.position + v;
            if (hit.transform != null)
            {
                position = hit.point;
                if (hit.transform.tag == "Player")
                {
                    Debug.Log(Time.time + "發現玩家");
                    zb.target = hit.transform;
                    zb.ChageState("Track");
                }
            }
            Debug.DrawLine(zb.eye.position, position, Color.red);
        }
    }

}
